package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.config.RabbitmqConfig;
import com.example.mapper.OrderMapper;
import com.example.model.OrderModel;
import com.example.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author lbq
 * @version 1.0
 * @date 2021/6/27 20:46
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderModel> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public String pay(Long id) {
        OrderModel model = orderMapper.selectById(id);
        if (model == null) {
            return "无法查询到相关订单信息";
        }
        Integer status = model.getStatus();
        switch (status) {
            case 0:
                model.setStatus(1);
                model.setUpdateTime(new Date());
                orderMapper.updateById(model);
                return "支付成功";
            case 1:
                return "订单已支付,请勿重复支付";
            case 2:
                return "订单已过期,支付失败";
            default:
                return "非法状态";
        }
    }

    @Override
    @RabbitListener(queues = RabbitmqConfig.DEAD_ORDER_STATUS_QUEUE_NAME)
    public void updateStatus(Long id) {
        OrderModel model = orderMapper.selectById(id);
        if (model == null) {
            return;
        }
        Integer status = model.getStatus();
        if (status == 0) {
            model.setStatus(2);
            model.setUpdateTime(new Date());
            System.out.println("订单: " + model.getId() + "支付超时");
            orderMapper.updateById(model);
        }
    }


}
